<template>
  <!-- 配置卡片 -->
  <div class="config-card" @click="selectCard" ref="card">
    <div class="card-operation" @click.stop="editConfig" @mouseover="showSetting" @mouseleave="hideSetting">
      <transition name = "fade">
        <icon-font v-show="selected && !setting" name="select" color="#3399dd" class="iconfont"></icon-font>
      </transition>
      <transition name = "fade">
        <icon-font v-show="!selected || setting" name="setting" color="#ccc" class="iconfont"></icon-font>
      </transition>
    </div>
    <div class="card-content">
      <div class="config-name">{{configForm.configName}}</div>
      <div class="config-detail">
        <div class="detail-item">
          <span class="detail-item-name">打包子路径</span>
          <span class="detail-item-value">{{configForm.buildChild}}</span>
        </div>
        <div class="detail-item">
          <span class="detail-item-name">本地项目路径</span>
          <span class="detail-item-value">{{configForm.buildPath}}</span>
        </div>
        <div class="detail-item">
          <span class="detail-item-name">服务器地址</span>
          <span class="detail-item-value">{{configForm.remoteAddress}}</span>
        </div>
        <div class="detail-item">
          <span class="detail-item-name">web服务器类型</span>
          <span class="detail-item-value">{{configForm.webServerType}}</span>
        </div>
        <div class="detail-item">
          <span class="detail-item-name">web服务器路径</span>
          <span class="detail-item-value">{{configForm.webServerPath}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { remote } from 'electron'
export default {
  props: {
    configForm: {
      type: Object,
      default: () => {
        return {
          configName: '',
          buildPath: '',
          buildChild: '',
          remoteAddress: '',
          username: '',
          password: '',
          webServerType: '',
          webServerPath: ''
        }
      }
    },
    // 已选择的列表
    selectList: {
      type: Array,
      default: []
    }
  },
  computed: {
    // 判断当前有无被选中
    selected () {
      return this.selectList.some(list => list._id === this.configForm._id)
    }
  },
  data () {
    return {
      setting: false
    }
  },
  mounted () {
    this.$refs.card.addEventListener('contextmenu', (e) => {
      e.preventDefault()
      const contexMenu = this.createContextMenu()
      contexMenu.popup({
        window: remote.getCurrentWindow()
      })
    }, false)
  },
  methods: {
    createContextMenu () {
      const { Menu } = remote
      const contextTemplate = [
        { label: '   编辑   ', click: () => this.editConfig() },
        { label: '   复制   ', click: () => this.copyConfig() },
        { label: '   删除   ', click: () => this.deleteConfig() }
      ]
      const contexMenu = Menu.buildFromTemplate(contextTemplate)
      return contexMenu
    },
    // 鼠标移入控制区，自动变成设置图标
    showSetting () {
      this.setting = true
    },
    hideSetting () {
      this.setting = false
    },
    // 选择/取消选择卡片
    selectCard () {
      this.$emit('select', this.configForm._id)
    },
    // 修改配置
    editConfig () {
      this.$emit('edit', JSON.parse(JSON.stringify(this.configForm)))
    },
    // 复制配置
    copyConfig () {
      this.$emit('copy', JSON.parse(JSON.stringify(this.configForm)))
    },
    // 删除配置
    deleteConfig () {
      this.$emit('delete', this.configForm._id)
    }
  }
}
</script>

<style lang="scss" scoped>
.config-card{
  display: flex;
  align-items: center;
  width: 100%;
  background: #fff;
  box-shadow: 2px 2px 10px #ccc;
  border-radius: 2px;
  overflow: hidden;
  cursor: pointer;
  transition: box-shadow linear .1s;
  &:hover{
    box-shadow: 2px 2px 14px #aaa;
  }
}
.card-operation{
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
}
.card-content{
  padding: 10px;
  width: calc(100% - 40px);
  height: 100%;
  box-sizing: border-box;
}
.config-name{
  padding: 5px 0 15px 0;
  color: #666;
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.detail-item{
  display: flex;
  margin-bottom: 5px;
  height: 22px;
  line-height: 1.1em;
  .detail-item-name{
    flex-shrink: 0;
    padding-right: 5px;
    width: 110px;
    color: #999;
    box-sizing: border-box;
  }
  .detail-item-value{
    color: #666;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
// 图标
.iconfont{
  position: absolute;
  font-size: 22px;
}
</style>